package net.sx.minemodule.mvp.presenters;

import android.app.Activity;
import android.databinding.ObservableField;
import android.util.Log;
import android.view.View;

import com.homegoods.login.ApiUtil;
import com.homegoods.login.KGetVerHelper;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.mvp.BaseView;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.StringUtil;
import com.kedll.library.util.ToastUtil;

import net.sx.minemodule.MineApiUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 修改支付密码
 */

public class MineUpdatePayPasswordPresenter extends BasePresenter<BaseView> {
    private static final String TAG = "MineUpdatePayPassword";
    private String mobile;

    /**
     * 验证码
     */
    public final ObservableField<String> strCode = new ObservableField<>();
    /**
     * 密码
     */
    public final ObservableField<String> strNewPwd = new ObservableField<>();
    private KGetVerHelper kGetVerHelper;

    public MineUpdatePayPasswordPresenter(KGetVerHelper kGetVerHelper) {
        this.kGetVerHelper = kGetVerHelper;
    }


    public void submit(View view) {
        //modify shibo.zheng start
        Log.d(TAG, "submit: MineUpdatePayPasswordPresenter moble == " + mobile );
        String authCode = strCode.get();
        String password = strNewPwd.get();
        Map<String, Object> map = new HashMap<>();
        map.put("telephone", mobile);
        map.put("password", password);
        map.put("authCode", authCode);
        if (checkData(view)) {
            /*MineApiUtils.modifyUserPhoneAndPwd("WithdrawalsPW", MyUtils.MD5(strNewPwd.get()), strCode.get(), s -> {
                ToastUtil.showToast("修改成功");
            }, throwable -> {
            });*/
//            MineApiUtils.modifyUserPhoneAndPwd(map, s -> {
//                ToastUtil.showToast("修改成功");
//            }, throwable -> {});
        }
        //modify shibo.zheng end
    }

    /**
     * 检查登录所需数据的合法性
     *
     * @return
     */
    private boolean checkData(View view) {
        String code = strCode.get();
        String pwd = strNewPwd.get();
        //验证码不能为空
        if (StringUtil.isEmpty(code)) {
            ToastUtil.showSnackbar(view, "验证码不能为空");
            return false;
        }
        //验证码不正确
        if (StringUtil.isEmpty(mobile)) {
            ToastUtil.showSnackbar(view, "验证码不正确");
            return false;
        }
        if (!StringUtil.checkPassword(pwd)) {
            ToastUtil.showSnackbar(view, "请输入6-16位数字，字母");
            return false;
        }
        return true;
    }

    /**
     * 获取短信验证码
     */
    public void getVerCode(String phone) {
        mobile = phone;
        MineApiUtils.getCaptchaForForgetPassWord(phone, jsonElement -> {
            kGetVerHelper.sendSuc();
        }, throwable -> {

        });
    }
}
